//
//  RegisterViewController.swift
//  issac-note
//
//  Created by Issac on 16/8/27.
//  Copyright © 2016年 Issac. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON

class RegisterViewController: UIViewController {

    @IBOutlet var username: UITextField!
    @IBOutlet var pwd_frist: UITextField!
    @IBOutlet var pwd_second: UITextField!
    @IBOutlet var email: UITextField!
    @IBOutlet var loading: UIActivityIndicatorView!
    @IBOutlet var signUp: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        signUp.layer.cornerRadius = 5
        self.loading.stopAnimating()
        
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    //MARK: - IBAction
    @IBAction func registerAction(sender: UIButton) {
        
        self.loading.startAnimating()
        
        if
            self.username.text   == "" ||
            self.pwd_frist.text  == "" ||
            self.pwd_second.text == "" ||
            self.email.text      == ""
        {
    
            self.loading.stopAnimating()
            self.alertTipAction("请输入完整信息")
            
        }else {
        
            if self.pwd_frist.text != self.pwd_second.text {
            
                self.alertTipAction("两次密码不一致")
                
            }else {
                
                registerByServer(username.text, pwd: pwd_frist.text, email: email.text)
                
            }
            
        }
        
        NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: #selector(delayLoadStop), userInfo: nil, repeats: false)
        
    }
    
    
    //MARK: - myself-function
    
    func registerByServer(user: String?, pwd: String?, email: String?) {
    
        let url: String = "http://issac.website/issac-noteBackstage/Admin/index/register"
        let parameters:[String:String] = [
            "user"  : user!,
            "pwd"   : pwd!,
            "email" : email!
        ]
        
        Alamofire.request(.POST, url, parameters: parameters).validate().responseJSON { response in
            
            switch response.result {
            
            case .Success:
                
                if let value = response.result.value {
                
                    let json = JSON(value)
                    if !json["status"] {
                    
                        self.alertTipAction("注册成功，青岛您的注册邮箱，激活账号")
                        
                    }else {
                    
                        self.alertTipAction("注册失败，请稍后再试")
                        
                    }
                    
                }
                
            case .Failure:
                
                self.alertTipAction("网络错误，请稍后再试")
                
            }
            
        }
        
    }
    
    func delayLoadStop(){
    
        self.loading.stopAnimating()
        
    }
    
    
    //alert提示
    func alertTipAction(message: String) {
    
        let alertAction = UIAlertController(title: "注册Issac-Note", message: message, preferredStyle: .Alert)
        let cancelAction = UIAlertAction(title: "确定", style: .Cancel, handler: nil)
        alertAction.addAction(cancelAction)
        self.presentViewController(alertAction, animated: true, completion: nil)
        
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
